﻿#define _CRT_SECURE_NO_WARNINGS 
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
	int i, j;
	for (i = 0;i < rows;i++) {
		for (j = 0;j < cols;j++) {
			board[i][j] = set;
		}
	}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i, j;
	for (i = 0;i <= row;i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1;i <= row;i++) {
		printf("%d ", i);
		for (j = 1;j <= col;j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");

	}
}
void Setboom(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
	int x = rand() % row + 1;
	int y = rand() % col + 1;
	if (board[x][y] == '0')
	{
	    board[x][y] = '1';
		count--;
	     }	
    } 
}
//字符'1'-字符'0'=数字1；
//同理’3‘-’0‘=3；
int get_boom_count(char boom[ROWS][COLS], int x, int y)
{
    return boom[x][y] +
		boom[x][y - 1] +
		boom[x - 1][y - 1] +
		boom[x - 1][y + 1] +
		boom[x + 1][y + 1] +
		boom[x + 1][y] +
		boom[x - 1][y] +
		boom[x + 1][y - 1] - 8 * '0'; //等于计算出周围有几颗雷；
}
void findboom(char boom[ROWS][COLS], char showbm[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	while (win<row*col-EASY_COUNT) {
		printf("请输入坐标：\n");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (boom[x][y] == '1') //踩中地雷
			{
				printf("什么狗屎运啊！这就踩中了地雷！\n");
				Displayboard(boom, row, col);//地雷出现；
				break;
			}
			else
			{
			 int count=get_boom_count(boom,x,y);//获取周围地雷的个数
			 showbm[x][y] = count + '0';
			 Displayboard(showbm, row, col);
			 win++;
			}
		}
		else printf("输入坐标非法！请重新输入！\n");
	}
	if (win == row * col - EASY_COUNT) {
		printf("恭喜！排雷成功，军队快速通过，夺取了胜利！\n");
		Displayboard(boom, row, col);
	}
}